package demo19;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Adder extends Thread{
    @Override
    public void run() {
        for (long i = 0; i < 10_000_000; i++) {
            LockDemo.lock.lock();
            try {
                LockDemo.n++;
            } finally {
                LockDemo.lock.unlock();
            }
        }
    }
}

class Suber extends Thread{
    @Override
    public void run() {
        for (long i = 0; i < 10_000_000; i++) {
            LockDemo.lock.lock();
            try {
                LockDemo.n--;
            } finally {
                LockDemo.lock.unlock();
            }
        }
    }
}

public class LockDemo {
    static Lock lock = new ReentrantLock();
    static int n = 0;

    public static void main(String[] args) throws InterruptedException {
        Adder adder = new Adder();
        adder.start();
        Suber suber = new Suber();
        suber.start();

        adder.join();
        suber.join();

        System.out.println(n);
    }

}
